package org.example;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class BeanDefination {
    private final String name;
    private final Class<?> beanType;
    private final Method postConstructMethod;
    private final List<Field> autowiredfieldList;
    private final Constructor<?> constructor;


    public BeanDefination(Class<?> type){
        this.beanType = type;
        Componet componet = type.getDeclaredAnnotation(Componet.class);
        this.name = componet.name().isEmpty() ? type.getSimpleName() : componet.name();

        try {
            this.constructor = type.getConstructor();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        this.autowiredfieldList = Arrays.stream(type.getDeclaredFields()).filter(f->f.isAnnotationPresent(Autowired.class)).toList();
        this.postConstructMethod = Arrays.stream(type.getDeclaredMethods()).filter(m->m.isAnnotationPresent(PostConstruct.class)).findFirst().orElse(null);
//        System.out.println("name :: " + name + "sz : " + autowiredfieldList.size());
    }

    public String getName() {
        return name;
    }

    public Class<?> getBeanType() {
        return beanType;
    }

    public Method getPostConstructMethod() {
        return postConstructMethod;
    }

    public List<Field> getAutowiredfieldList() {
        return autowiredfieldList;
    }

    public Constructor<?> getConstructor() {
        return constructor;
    }
}
